﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Controllers;

namespace Web
{
    public partial class DailyEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string d = Request.QueryString["date"];
            lblDate.InnerText = d;
            if(!IsPostBack)
            {
                for(int i=8;i<=23;i++)
                {
                    ddlStart0.Items.Add(new ListItem(i.ToString(), i.ToString()));
                    ddlStart1.Items.Add(new ListItem(i.ToString(), i.ToString()));
                    ddlStart2.Items.Add(new ListItem(i.ToString(), i.ToString()));
                    ddlStart3.Items.Add(new ListItem(i.ToString(), i.ToString()));
                    ddlStart4.Items.Add(new ListItem(i.ToString(), i.ToString()));
                    ddlEnd0.Items.Add(new ListItem(i.ToString(), i.ToString()));
                    ddlEnd1.Items.Add(new ListItem(i.ToString(), i.ToString()));
                    ddlEnd2.Items.Add(new ListItem(i.ToString(), i.ToString()));
                    ddlEnd3.Items.Add(new ListItem(i.ToString(), i.ToString()));
                    ddlEnd4.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                for (int i = 0; i <= 7; i++)
                {
                    ddlStart0.Items.Add(new ListItem(i.ToString(), i.ToString()));
                    ddlStart1.Items.Add(new ListItem(i.ToString(), i.ToString()));
                    ddlStart2.Items.Add(new ListItem(i.ToString(), i.ToString()));
                    ddlStart3.Items.Add(new ListItem(i.ToString(), i.ToString()));
                    ddlStart4.Items.Add(new ListItem(i.ToString(), i.ToString()));
                    ddlEnd0.Items.Add(new ListItem(i.ToString(), i.ToString()));
                    ddlEnd1.Items.Add(new ListItem(i.ToString(), i.ToString()));
                    ddlEnd2.Items.Add(new ListItem(i.ToString(), i.ToString()));
                    ddlEnd3.Items.Add(new ListItem(i.ToString(), i.ToString()));
                    ddlEnd4.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                ddlStart0.Value = "8";
                ddlStart1.Value = "8";
                ddlStart2.Value = "8";
                ddlStart3.Value = "8";
                ddlStart4.Value = "8";
                ddlEnd0.Value = "18";
                ddlEnd1.Value = "18";
                ddlEnd2.Value = "18";
                ddlEnd3.Value = "18";
                ddlEnd4.Value = "18";

                if(!string.IsNullOrEmpty(d))
                {
                    EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
                    ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                    IList<Daily> dailyList = (from r in ctx.Dailies
                                             where r.EmpId == ebi.Id && r.CreatDate == DateTime.Parse(d) orderby r.StartTime
                                             select r).ToList();
                    for(int i=0;i<dailyList.Count();i++)
                    {
                        ((HtmlTextArea)this.FindControl("txtRecord" + i)).Value = dailyList[i].Record;
                        ((HtmlSelect)this.FindControl("ddlStart" + i)).Value = dailyList[i].StartTime.ToString();
                        ((HtmlSelect)this.FindControl("ddlEnd" + i)).Value = dailyList[i].EndTime.ToString();
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string d = Request.QueryString["date"];
            EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            IQueryable<Daily> dailyList = from r in ctx.Dailies
                                      where r.EmpId == ebi.Id && r.CreatDate == DateTime.Parse(d)
                                      select r;
            ctx.Dailies.DeleteAllOnSubmit(dailyList);
            for (int i = 0; i <5; i++)
            {
                if(((HtmlTextArea)Page.FindControl("txtRecord" + i)).Value!="")
                {
                    Daily daily = new Daily();
                    daily.EmpId = ebi.Id;
                    daily.CreatDate = DateTime.Parse(d);
                    daily.Record = ((HtmlTextArea)this.FindControl("txtRecord" + i)).Value;
                    daily.StartTime = int.Parse(((HtmlSelect)this.FindControl("ddlStart" + i)).Value);
                    daily.EndTime = int.Parse(((HtmlSelect)this.FindControl("ddlEnd" + i)).Value);
                    ctx.Dailies.InsertOnSubmit(daily);
                }
            }
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='Succeed';window.close();</script>");
        }
    }
}